const formEle = document.querySelector('form')



function getLoginInfo() {
    let username = document.querySelector('input[name="username"]')
    let password = document.querySelector('input[name="password"]')
    username = username.value
    password = password.value
    console.log(password);
    myAjaxPromise({
        url: 'http://api.yuguoxy.com/api/login',
        method: 'POST',
        data: {
            username,
            password
        },
    }).then(res => {
        if(res.resultCode == 1){
            let info = res.resultInfo
            localStorage.setItem('USER',JSON.stringify(info.username))
            location.href = './index.html'
            console.log(info);
        }else{
            alert(res.resultInfo)
        }
        
    }).catch(err => {
        console.log(err);
    })

}

function login() {
    formEle.addEventListener('submit',function (e) {
        e = e || window.event
        e.preventDefault()
        // alert('ok')
        getLoginInfo()
        

    })


} 
// 账号非空
function nameNull() {  
    let usernameP = document.querySelector('.name')
    let username = document.querySelector('input[name="username"]')

    if (username.value == '') {
        usernameP.innerHTML = '账号不能为空！'
    }else{
        usernameP.innerHTML = ''
    }
    
} 
// 密码非空
function passwordNull() { 
    let password = document.querySelector('input[name="password"]')
    let passwordP = document.querySelector('.password')
    if (password.value == '') {
        passwordP.innerHTML = '密码不能为空！'
    }else{
        passwordP.innerHTML = ''
    }
}
// 失去焦点
function loseFocus() {
    let username = document.querySelector('input[name="username"]')
    let password = document.querySelector('input[name="password"]')
    username.onblur = function () {
        nameNull()
    }
    password.onblur = function () {
        passwordNull()
    }
}
// 跳转到注册界面
function goLogon() {
    
    let logonEle = document.querySelector('.logon')
    logonEle.onclick = function () {
        location.href = './logon.html'
    }
}
login()
loseFocus()
goLogon()
